package com.blockchain.console.service;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.blockchain.console.config.ConsoleConst;
import com.blockchain.framework.data.sql.SQLConnectionProvider;
import com.blockchain.framework.resource.ResourceNotFoundException;
import com.blockchain.framework.service.ServiceResource;
import com.blockchain.framework.service.query.ItemParser;
import com.blockchain.service.AbstractP2PService;

public abstract class AbstractConsoleService extends AbstractP2PService {

	public AbstractConsoleService(ServiceResource serviceResource) {
		super(serviceResource);
	}

	protected SQLConnectionProvider getSQLConnectionProvider()
			throws ResourceNotFoundException, SQLException {
		return serviceResource.getDataConnectionProvider(
				SQLConnectionProvider.class, ConsoleConst.DB_MASTER_PROVIDER);
	}

	protected Connection getConnection() throws ResourceNotFoundException,
			SQLException {
		return serviceResource.getDataConnectionProvider(
				SQLConnectionProvider.class, ConsoleConst.DB_MASTER_PROVIDER)
				.getConnection(ConsoleConst.DB_CONSOLE);
	}

	protected Connection getConnection(String db)
			throws ResourceNotFoundException, SQLException {
		return serviceResource.getDataConnectionProvider(
				SQLConnectionProvider.class, ConsoleConst.DB_MASTER_PROVIDER)
				.getConnection(db);
	}
	
	
	protected Integer selectInt(String db, String sql,
			Object... paramters) throws SQLException {
		final Integer decimal = new Integer(0);
		return select(getConnection(db), new ItemParser<Integer>() {
			@Override
			public Integer parse(ResultSet resultSet) throws SQLException {
				if (resultSet.next()) {
					return resultSet.getInt(1);
				}
				return decimal;
			}
		}, sql, paramters);
	}
	protected BigDecimal selectBigDecimal(String db, String sql,
			Object... paramters) throws SQLException {
		final BigDecimal decimal = new BigDecimal(0);
		return select(getConnection(db), new ItemParser<BigDecimal>() {
			@Override
			public BigDecimal parse(ResultSet resultSet) throws SQLException {
				if (resultSet.next()) {
					return resultSet.getBigDecimal(1);
				}
				return decimal;
			}
		}, sql, paramters);
	}
	protected String selectString(String db, String sql,
			Object... paramters) throws SQLException {
		final String decimal = new String();
		return select(getConnection(db), new ItemParser<String>() {
			@Override
			public String parse(ResultSet resultSet) throws SQLException {
				if (resultSet.next()) {
					return resultSet.getString(1);
				}
				return decimal;
			}
		}, sql, paramters);
	}
	
	//记录操作日志
	protected void log(int accountId, String content) throws Throwable {
		String accountName = "";
		String updateT7011 = "UPDATE T7011 SET F07=CURRENT_TIMESTAMP() WHERE F01=?";
		try (Connection connection = getConnection()) {
			try (PreparedStatement ps = connection
					.prepareStatement("SELECT F02 FROM T7011 WHERE F01=?")) {
				ps.setInt(1, accountId);
				try (ResultSet rs = ps.executeQuery()) {
					if (rs.next()) {
						accountName = rs.getString(1);
					}
				}
			}
			try (PreparedStatement ps = connection
					.prepareStatement("INSERT INTO T7012 SET F02 = ?,F03=CURRENT_TIMESTAMP(), F04 = ?")) {
				ps.setString(1, accountName);
				ps.setString(2, content);
				ps.executeUpdate();
			}
		}
		execute(getConnection(), updateT7011,accountId);
	}
}
